package com.decoder.util;

import java.util.ArrayList;
import java.util.Date;

import com.decoder.bean.Addivieinfo;



public class InformationDataLoader {

	private boolean isLoading = false;
	
	ILoadDataResponse responseInterface;

	public InformationDataLoader(ILoadDataResponse responseInterface) {
		this.responseInterface = responseInterface;
	}

	public boolean isLoading() {
		return isLoading;
	}

	public void loadNext() {
		new Thread() {

			@Override
			public void run() {
				super.run();
				isLoading = true;
				try {
					Thread.sleep(5000);//sleep fine seconds to simulate fetching data
				} catch (InterruptedException e) {
					e.printStackTrace();
				}finally{
					isLoading = false;
				}
				if(Math.random() < 0.03){
					//retrieving data from server may fail sometime,
					//just to simulate this kind of occasion;
					responseInterface.onLoadDataError("somehow It failed");
				}else{
					ArrayList<Addivieinfo> infos = new ArrayList<Addivieinfo>();
					//we don't want to generate more data then the total number 
					for(int i=0; i < 10; i++) {
						Addivieinfo info = new Addivieinfo();
						info.setAddivieInfoTitle("title" + (i+1));
						info.setAddivieInfoContent("content" + (i+1));
						info.setInfoPublishTime(new Date());
						infos.add(info);
					}
					//mark the number of the data we have generated,
					//this is all for demonstrating purpose
					//totally USELESS in real project.//TODO 
					responseInterface.onLoadDataComplete(infos, 0, 0);
				}
			}
			
		}.start();
	}
	
}
